﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Domain.Values
{
	using System;
	using System.Web;
	using System.Web.Mvc;
	using Falcon.Core.Domain.Enums;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;
	using Falcon.Core.Validators;

	public class UserBadgeUpdate : ValueBase<UserBadgeUpdate>
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="UserBadgeUpdate"/> class.
		/// </summary>
		public UserBadgeUpdate()
		{
			
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="UserBadgeUpdate"/> class.
		/// </summary>
		/// <param name="badge">The badge.</param>
		public UserBadgeUpdate(UserBadge badge) : this()
		{
			this.Id = badge.Id;
			this.Name = badge.User.Name;
			this.Title = badge.Title;
			this.TitleBefore = badge.Title;
			this.Body = badge.Body;
			this.Reputation = badge.Reputation;
			this.ReputationBefore = badge.Reputation;
			this.FilePath = badge.FilePath;
			this.Scope = badge.Scope;
			this.Change = UserBadgeChange.Multiple;
		}

		/// <summary>
		/// Gets or sets the id.
		/// </summary>
		/// <value>The id.</value>
		public int Id
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the name.
		/// </summary>
		/// <value>The name.</value>
		public string Name
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the title.
		/// </summary>
		/// <value>The title.</value>
		public string Title
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the title before.
		/// </summary>
		/// <value>The title before.</value>
		public string TitleBefore
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the body.
		/// </summary>
		/// <value>The body.</value>
		public string Body
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the file path.
		/// </summary>
		/// <value>The file path.</value>
		public string FilePath
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the reputation.
		/// </summary>
		/// <value>The reputation.</value>
		public int Reputation
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the reputation before.
		/// </summary>
		/// <value>The reputation before.</value>
		public int ReputationBefore
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets a value indicating whether [title changed].
		/// </summary>
		/// <value><c>true</c> if [title changed]; otherwise, <c>false</c>.</value>
		public bool TitleChanged
		{
			get
			{
				return (this.Title != this.TitleBefore);
			}
		}

		/// <summary>
		/// Gets a value indicating whether [reputation changed].
		/// </summary>
		/// <value><c>true</c> if [reputation changed]; otherwise, <c>false</c>.</value>
		public bool ReputationChanged
		{
			get
			{
				return (this.Reputation != this.ReputationBefore);
			}
		}

		/// <summary>
		/// Gets or sets the scope.
		/// </summary>
		/// <value>The scope.</value>
		public UserBadgeScope Scope
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the change.
		/// </summary>
		/// <value>The change.</value>
		public UserBadgeChange Change
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the source.
		/// </summary>
		/// <value>The source.</value>
		public HttpPostedFileBase Source
		{
			get;
			set;
		}

		/// <summary>
		/// Gets the scopes.
		/// </summary>
		/// <value>The scopes.</value>
		public SelectList Scopes
		{
			get
			{
				return new SelectList(Enum.GetNames(typeof(UserBadgeScope)), this.Scope.Name());
			}
		}

		/// <summary>
		/// Gets the changes.
		/// </summary>
		/// <value>The changes.</value>
		public SelectList Changes
		{
			get
			{
				return new SelectList(Enum.GetNames(typeof(UserBadgeChange)), this.Change.Name());
			}
		}

		/// <summary>
		/// Validation rules for this instance.
		/// </summary>
		/// <returns></returns>
		public override Validator<UserBadgeUpdate> Rules()
		{
			var rules = base.Rules();

			rules.Add(x => x.Name)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Name));

			rules.Add(x => x.Name)
				.Custom(new UserNameMustExistValidator(), Resources.Errors.UserNameNotFound);

			rules.Add(x => x.Title)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Title));

			if (this.TitleChanged)
			{
				rules.Add(x => x.Title)
					.Custom(new UserBadgeUniqueTitleValidator(), Resources.Errors.UserBadgeTitleTaken);
			}

			return base.Rules();
		}

		/// <summary>
		/// Validates this instance.
		/// </summary>
		public override void Validate()
		{
			this.Rules().Validate(this);
		}
	}
}